package mta.selector;

import mta.localization.LocalizationPackage;
import mta.localization.ModulePhrase;

import java.util.*;

/**
 * Magento Translation Assistant
 * Copyright (c) 2009 Dmitry Nikolaev
 * Contact author: blackliteon@gmail.com
 */
public class TemplateSelector implements PhraseSelector {

    private LocalizationPackage loc;
    private String template;

    public TemplateSelector(LocalizationPackage loc, String template) {
        this.loc = loc;
        this.template = template;
    }

    private List<String> getAllOriginalThatMatches() {
        List<String> result = new ArrayList<String>();
        String template = this.template.toLowerCase();

        for (ModulePhrase mPhrase : loc.getAllModulePhrases()) {
            if (loc.hasTranslationForModulePhrase(mPhrase) && !result.contains(mPhrase.getPhrase())) {
                String translation = loc.getTranslationForModulePhrase(mPhrase).toLowerCase();

                if (translation.indexOf(template) >= 0) {
                    result.add(mPhrase.getPhrase());
                }
            }
        }

        return result;
    }

    public HashMap<String, String> getPhrases() {
        LinkedHashMap<String, String> result = new LinkedHashMap<String, String>();

        List<String> allOriginalsMatches = getAllOriginalThatMatches();
        Collections.sort(allOriginalsMatches);

        for (String original : allOriginalsMatches) {
            result.put(original, loc.getTranslation(original));
        }


        return result;
    }
}
